package io.renren.modules.base.controller;

import java.util.List;
import java.util.Map;
import java.util.UUID;

import io.renren.common.annotation.SysLog;
import io.renren.common.utils.IdGen;
import io.renren.modules.base.entity.BaseTaskInfoEntity;
import io.renren.modules.base.entity.BaseTaskWorkitemEntity;
import io.renren.modules.base.service.BaseTaskWorkitemService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.base.entity.BaseWorkitemInfoEntity;
import io.renren.modules.base.service.BaseWorkitemInfoService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;




/**
 * 
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-10-05 17:55:20
 */
@RestController
@RequestMapping("/generator/baseworkiteminfo")
public class BaseWorkitemInfoController {
	@Autowired
	private BaseWorkitemInfoService baseWorkitemInfoService;
	@Autowired
	private BaseTaskWorkitemService baseTaskWorkitemService;

	@SysLog("新增模板工作项")
	@RequestMapping("/add/{workitemName}/{deliveryGoods}/{taskId}")
	public R add(@PathVariable("workitemName") String workitemName,@PathVariable("deliveryGoods") String deliveryGoods,@PathVariable("taskId") String taskId){
		BaseWorkitemInfoEntity baseWorkitemInfoEntity = new BaseWorkitemInfoEntity();
		baseWorkitemInfoEntity.setWorkitemName(workitemName);
		baseWorkitemInfoEntity.setDeliveryGoods(deliveryGoods);
		baseWorkitemInfoEntity.setTaskId(taskId);
		baseWorkitemInfoService.save(baseWorkitemInfoEntity);
		return R.ok();
	}

	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("generator:baseworkiteminfo:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<BaseWorkitemInfoEntity> baseWorkitemInfoList = baseWorkitemInfoService.queryList(query);
		int total = baseWorkitemInfoService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(baseWorkitemInfoList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("generator:baseworkiteminfo:info")
	public R info(@PathVariable("id") String id){
		BaseWorkitemInfoEntity baseWorkitemInfo = baseWorkitemInfoService.queryObject(id);
		
		return R.ok().put("baseWorkitemInfo", baseWorkitemInfo);
	}
	
	/**
	 * 保存
	 */
	@SysLog("新增模板工作项")
	@RequestMapping("/save")
	@RequiresPermissions("generator:baseworkiteminfo:save")
	public R save(@RequestBody BaseWorkitemInfoEntity baseWorkitemInfo){
		baseWorkitemInfo.setId(IdGen.uuid());
		baseWorkitemInfoService.save(baseWorkitemInfo);
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@SysLog("修改模板工作项")
	@RequestMapping("/update/{id}/{workitemName}/{deliveryGoods}")
	@RequiresPermissions("generator:baseworkiteminfo:update")
	public R update(@PathVariable("id") String id, @PathVariable("workitemName") String workitemName,@PathVariable("deliveryGoods") String deliveryGoods){
		BaseWorkitemInfoEntity baseWorkitemInfo = new BaseWorkitemInfoEntity();
		baseWorkitemInfo.setId(id);
		baseWorkitemInfo.setDeliveryGoods(deliveryGoods);
		baseWorkitemInfo.setWorkitemName(workitemName);
		baseWorkitemInfoService.update(baseWorkitemInfo);
		
		return R.ok();
	}

	/**
	 * 修改
	 */
	@SysLog("修改模板工作项")
	@RequestMapping("/updateWorkitemInfo")
	public R updateWorkitemInfo(@RequestBody BaseWorkitemInfoEntity baseWorkitemInfo){
		baseWorkitemInfoService.updateWorkitemInfo(baseWorkitemInfo);

		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("generator:baseworkiteminfo:delete")
	public R delete(@RequestBody String[] ids){
		baseWorkitemInfoService.deleteBatch(ids);
		
		return R.ok();
	}

	@SysLog("删除模板工作项")
	@RequestMapping("/deleteWorkitem/{workitemId}")
	public R deleteTask(@PathVariable("workitemId") String workitemId){
		baseWorkitemInfoService.deleteWorkitem(workitemId);

		return R.ok();
	}
	
}
